<script setup>
import { ref, onUnmounted, nextTick } from "vue";
import {
  suppapiLogin,
  login,
  send,
  mini,
  commonPhone,
  appLogin,
} from "@/services/login";
import { useUserStore } from "@/stores/user.js";
import { userloginStore } from "@/stores/islogin.js";

import { skip } from "@/services/user";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { isMobile } from "@/utils/isMobile";
import { productinfo } from "@/services/thali";
import { handleGoBack } from "@/utils/router";
import loginto from "@/utils/reder.js";

const shop = ref("");
const password = ref("");
const mobile = ref("");
const shopshow = ref(false);

if (uni.getStorageSync("mobile")) {
  mobile.value = uni.getStorageSync("mobile");
}

if (uni.getStorageSync("shop")) {
  shop.value = uni.getStorageSync("shop");
}

if (uni.getStorageSync("mobile")) {
  shop.value = uni.getStorageSync("mobile");
}
const onSubmit = async (values) => {
  // if (!values.shop) {
  //   return showToast("请输入店铺");
  // }
  console.log(values, mobile.value);
  if (!mobile.value) {
    uni.showToast({
      title: "请输入手机号",
      icon: "none",
    });
    return;
  }
  if (!password.value) {
    uni.showToast({
      title: "请输入密码",
      icon: "none",
    });
    return;
  }
  if (!protocol.value) {
    uni.showToast({
      title: "请阅读和勾选服务协议和隐私政策",
      icon: "none",
    });
    return;
  }
  try {
    let res = await appLogin(values);
    console.log(res);

    if (res.code == 200) {
      uni.setStorageSync("isPass", true);
      uni.showToast({
        title: "登录成功",
        icon: "none",
      });
      if (res.data.employees.length >= 1) {
        uni.setStorageSync("more", 1);
      } else {
        uni.setStorageSync("more", 0);
      }

      uni.setStorageSync("X-Auth-Token", res.data.token);
      uni.setStorageSync("user", JSON.stringify(res.data.user));

      if (res.data.user.adminPermission) {
        uni.setStorageSync("shopType", res.data.user.adminPermission ? 2 : 1);
        uni.setStorageSync(
          "selectShopType",
          res.data.user.adminPermission ? "admin" : "shop"
        );
      } else {
        uni.setStorageSync("shopType", res.data.user.adminPermission ? 2 : 1);
        uni.setStorageSync(
          "selectShopType",
          res.data.user.adminPermission ? "admin" : "shop"
        );
      }

      if (!res.data.user.adminPermission) {
        if (res.data.employees && res.data.employees.length > 1) {
          setTimeout(() => {
            uni.reLaunch({ url: "/pages/users/switch/index" });
          }, 1000);
        } else {
          setTimeout(() => {
            uni.reLaunch({ url: "/pages/index/index" });
          }, 1000);
        }
      } else {
        if (res.data.user.adminPermission && res.data.user.storePermission) {
          uni.reLaunch({
            url: "/pages/users/switch-word/index?isSwitch=1",
          });
        } else {
          setTimeout(() => {
            uni.reLaunch({ url: "/pages/index/index" });
          }, 1000);
        }
      }

      uni.setStorageSync("mobile", values.mobile);
    }
  } catch (e) {
    console.log(e);
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
    return;
  }
};

const onSubmit2 = async () => {
  if (!protocol.value) {
    uni.showToast({
      title: "请阅读和勾选服务协议和隐私政策",
      icon: "none",
    });
    return;
  }
  // if (!values.shop) {
  //   return showToast("请输入店铺");
  // }

  try {
    let res = await appLogin({ password: "123456", mobile: "13700000001" });

    if (res.code == 200) {
      uni.setStorageSync("isPass", false);
      uni.showToast({
        title: "登录成功",
        icon: "none",
      });
      if (res.data.employees.length >= 1) {
        uni.setStorageSync("more", 1);
      } else {
        uni.setStorageSync("more", 0);
      }

      uni.setStorageSync("X-Auth-Token", res.data.token);
      uni.setStorageSync("user", JSON.stringify(res.data.user));

      if (res.data.user.adminPermission) {
        uni.setStorageSync("shopType", res.data.user.adminPermission ? 2 : 1);
        uni.setStorageSync(
          "selectShopType",
          res.data.user.adminPermission ? "admin" : "shop"
        );
      } else {
        uni.setStorageSync("shopType", res.data.user.adminPermission ? 2 : 1);
        uni.setStorageSync(
          "selectShopType",
          res.data.user.adminPermission ? "admin" : "shop"
        );
      }

      if (!res.data.user.adminPermission) {
        if (res.data.employees && res.data.employees.length > 1) {
          setTimeout(() => {
            uni.reLaunch({ url: "/pages/users/switch/index" });
          }, 1000);
        } else {
          setTimeout(() => {
            uni.reLaunch({ url: "/pages/index/index" });
          }, 1000);
        }
      } else {
        if (res.data.user.adminPermission && res.data.user.storePermission) {
          uni.reLaunch({
            url: "/pages/users/switch-word/index?isSwitch=1",
          });
        } else {
          setTimeout(() => {
            uni.reLaunch({ url: "/pages/index/index" });
          }, 1000);
        }
      }

      // uni.setStorageSync("mobile", values.mobile);
    }
  } catch (e) {
    console.log(e);
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
    return;
  }
};
// #ifdef MP-WEIXIN
const title = ref("用户隐私保护提示");
const desc1 = ref("感谢您使用膳友会小程序，您使用本小程序前应当阅读并同意");
const urlTitle = ref("《用户隐私保护指引》");
const desc2 = ref(
  "当您点击同意并开始时用产品服务时，即表示你已理解并同息该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法正常使用该小程序。"
);

if (uni.getPrivacySetting) {
  uni.getPrivacySetting({
    success: (res) => {
      if (res.needAuthorization) {
        nextTick(() => {
          setTimeout(() => {
            popup.value.open("center");
          }, 1);
        });
      } else {
      }
    },
    fail: () => {},
    complete: () => {},
  });
} else {
  // 低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用
}
// #endif
const close = () => {};
const open = () => {};

const isPass = ref(true);
const onLogin = () => {
  isPass.value = !isPass.value;
};
const protocol = ref(false);
const ChangeIsDefault = (e) => {
  console.log(e);
  protocol.value = !protocol.value;
};
const privacy = (index) => {
  if (index == 3) {
    uni.navigateTo({
      url: "/pages/user_about/index?from=sys_user_agree",
    });
  }
  if (index == 5) {
    uni.navigateTo({
      url: "/pages/user_about/index?from=sys_user_privacy",
    });
  }
};
</script>
<template>
  <view class="login">
    <view class="bg">
      <div class="ul-img"></div>
      <view class="ul">
        <view class="li">
          <image src="../../static/login/logo.png" mode="widthFix"></image>
          <!-- <image src="../../static/login/logo2.png" mode="widthFix"></image> -->
        </view>
      </view>
      <view class="login-item-title">{{ "佰淘零售管理系统" }}</view>
    </view>

    <view>
      <view class="login-bottom">
        <!-- <view class="login-select">
          <view
            class="login-select-item"
            :class="isPass ? 'login-select-item-pass' : ''"
          >
            账号密码登录
          </view>
          <view
            class="login-select-item"
            :class="!isPass ? 'login-select-item-pass' : ''"
          >
            体验授权登录
          </view>
        </view> -->

        <view class="login-item-title-to" v-if="isPass">
          <up-input
            v-model="mobile"
            placeholder="请输入手机号"
            prefixIcon="../../static/login/in_b.png"
            class="login-item-title-input"
            prefixIconStyle="font-size: 48rpx;color: #909399;margin-left: 10rpx"
            border="none"
          ></up-input>
        </view>
        <view class="login-item-title-to">
          <up-input
            v-if="isPass"
            v-model="password"
            type="password"
            class="login-item-title-input"
            placeholder="请输入密码"
            prefixIcon="../../static/login/in_c.png"
            prefixIconStyle="font-size: 48rpx;color: #909399;margin-left: 10rpx"
            border="none"
          ></up-input>

          <button
            v-if="isPass"
            class="submit-index"
            @click="onSubmit({ password: password, mobile: mobile })"
          >
            登录
          </button>

          <button v-if="!isPass" class="submit-index" @click="onSubmit2()">
            体验用户授权
          </button>
        </view>
        <view class="xy">
          <checkbox-group @change="ChangeIsDefault" class="checkbox">
            <checkbox :checked="protocol" style="transform: scale(0.7)" />
            <text @click.stop="ChangeIsDefault">已阅读并同意</text>

            <text class="main-color" @click.stop="privacy(3)"
              >《服务协议》</text
            >
            、<text class="main-color" @click.stop="privacy(5)"
              >《隐私政策》</text
            >
          </checkbox-group>
        </view>
        <!-- <view class="login-two">
          <view class="login-two-content">
            <view class="login-two-icon">
              <up-icon name="man-add" color="#bfbfbf" size="80rpx"></up-icon>
            </view>
            <view class="login-two-text">体验授权</view>
          </view>
        </view> -->

        <!-- <button
          class="submit-index"
          @click="onSubmit2({ password: password, mobile: mobile })"
        >
          体验授权登录
        </button> -->

        <!-- #ifdef APP-PLUS -->

        <view class="type"
          ><text @click="onLogin()"
            >{{ isPass ? "切换体验授权登录" : "切换账号密码登录" }}
          </text>
        </view>
        <!-- #endif -->
        <view class="text-to">
          <image src="@/static/index/zone.png" mode="scaleToFill" /><text
            >零壹聚提供技术支持</text
          >
        </view>
      </view>
    </view>
  </view>

  <!-- #ifdef MP-WEIXIN -->

  <uni-popup ref="popup" :animation="true" :is-mask-click="false">
    <view class="popup_content">
      <view class="popup_title">
        {{ title }}
      </view>
      <view class="popup_desc">
        {{ desc1 }}
      </view>
      <view class="popup_urlTitle" @click="openPrivacyContract">
        {{ urlTitle }}
      </view>
      <view class="popup_desc">
        {{ desc2 }}
      </view>
      <view class="popup_cz">
        <button
          id="disagree-btn"
          type="default"
          class="weui-btn"
          @click="handleDisagree"
        >
          不同意
        </button>
        <button
          id="agree-btn"
          type="primary"
          open-type="agreePrivacyAuthorization"
          class="weui-btn"
          @agreeprivacyauthorization="handleAgree"
        >
          同意并继续
        </button>
      </view>
    </view>
  </uni-popup>
  <!-- #endif -->
</template>

<style lang="scss" scoped>
@import "login.scss";
</style>
